/* Gluten Library -- Reports
 *
 * A simple reporting utility. Outputs time-of messages to a list of registered
 * listeners.
 *
 * $AUTHOR$    res
 * $UPDATE$    r36
 */

/* LICENSES ********************************************************************

Copyright (c) 2013- Reuben E. Smith

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*******************************************************************************/


#ifndef GLUTEN_REPORTS_HPP
#define GLUTEN_REPORTS_HPP

#include <list>
#include <ostream>
#include <string>
#include "Utility.hpp"


namespace Gluten
{
   //! \struct ReportsItem
   //! \brief Describes a report item.
   struct ReportsItem
   {
      uintmax_t time;         // in ms
      std::string category;   // user specific
      std::string source;     // user specific
      std::string message;    // user specific


      friend std::ostream& operator<<(
         std::ostream& stream, const ReportsItem& report)
      {
         stream << std::fixed
                << '[' << (report.time / 1000.0) << "] "
                << '(' << report.category << ") "
                << report.source << " -- "
                << report.message << '\n';

         return stream;
      }
   };


   //! \class Reports
   //! \brief Handles report listener registration and sending.
   class Reports
   {
   public:
      //! \brief Send a time-of report to listeners.
      //! \param[in]    cat            user-defined category of report
      //! \param[in]    src            user-defined source of report
      //! \param[in]    msg            report contents
      void operator()(
         const std::string& cat, 
         const std::string& src, 
         const std::string& msg)
      {
         ReportsItem report;
         report.time = Gluten::getProgramTime();
         report.category = cat;
         report.source = src;
         report.message = msg;

         for (auto i = _listeners.begin(), j = _listeners.end(); i != j; ++i) {
            *(*i) << report;
         }
      }


      //! \brief Register a listener with the reports system.
      //! \param[in]    stream         listener to add
      void addListener(std::ostream* stream)
      {
         _listeners.push_back(stream);
      }


      //! \brief Unregister a listener with the reports system.
      //! \param[in]    stream         listener to remove
      void removeListener(std::ostream* stream)
      {
         _listeners.remove(stream);
      }


   private:
      std::list< std::ostream* > _listeners;
   };


   //! \var g_reports
   //! \brief Global reports handler.
   extern Reports g_reports;
}

#endif
